// pages/activity/index.js
const app = getApp();
const { PageBase } = require('../../mixins/page');
const { MARKET_COUPON_STATUS } = require('../../constants/market');
const { ACTIVITY_TYPES } = require('../../constants/types');
const { getScanCouponInfo, getScanCoupon, getCouponList } = require('../../api/market');
const { getUserInfo, setUserInfo } = require('../../biztools/info');
const { PAGE_NAV } = require('../../constants/route');
const { hideLoading, navTo, showLoading, tipToast } = require('../../common/platform');
const { userBindMobileWechat } = require('../../api/member');

Page({
    mixins: [PageBase],
    data: {
        isHave: false,
        isNeedBindMobile: false,
        memberInfo: getUserInfo(),
        tipText: '你有一张专属优惠券待领取',
        couponInfo: {
            id: 1, // id
            name: '专属优惠券', // 名字
            value: 100, // 优惠金额或优惠折扣
            valueTip: '满100可用', // 优惠提示
            isDiscount: false, // 是否是折扣
            validString: '2020.09.09 至 2022.09.09', // 有效期
        },
        couponAvalible: true,
        activityInfo: {},
    },
    async onLoad(options) {
        const { type, id } = options;
        await this.getCouponOrActivityInfo(type, id);
    },
    // 获取优惠券，活动信息
    async getCouponOrActivityInfo(type, id) {
        const { isLogin } = app.globalData;
        if (!isLogin) {
            setTimeout(async () => await this.getCouponOrActivityInfo(type, id), 200);
            return;
        }
        showLoading('加载中..');
        switch(type) {
            case ACTIVITY_TYPES.coupon:
                const { available, couponInfo } = await getScanCouponInfo(id);
                this.setData({
                    couponInfo,
                    couponAvalible: available,
                }, async () => await this.dealConfigInfo());
                break;
            case ACTIVITY_TYPES.activity:
                break;
        }
        hideLoading();
    },
    // 处理用户是否需要绑定手机号，是否领取过等信息
    async dealConfigInfo() {
        showLoading('获取信息..');
        const { couponInfo, memberInfo, couponAvalible } = this.data;
        const list = await getCouponList(MARKET_COUPON_STATUS.normal);
        const isHave = list.filter(item => item.id === couponInfo.id).length > 0;
        const { isNeedMobile } = couponInfo;
        this.setData({
            isHave,
            isNeedBindMobile: isNeedMobile ? memberInfo.mobile != '' : false,
            tipText: couponAvalible ? '优惠券已过期/已失效' : (isHave ? '您已领取优惠券' : '你有一张专属优惠券待领取'),
        });
        hideLoading();
    },
    // 领取优惠券
    async handleGetCoupon() {
        const { couponInfo } = this.data;
        showLoading('领取中..');
        const flag = await getScanCoupon(couponInfo.id);
        if (flag) {
            tipToast('领取成功！');
            await this.dealConfigInfo();
        } else {
            tipToast('领取失败，请重试！');
            hideLoading();
        }
    },
    // 跳转到优惠券列表
    gotoCouponList() {
        navTo(PAGE_NAV.couponList);
    },
    async getUserPhoneNumber(e) {
        const { iv, encryptedData } = e.detail;
        const memberInfo = await userBindMobileWechat(iv, encryptedData);
        setUserInfo(memberInfo);
        this.setData({
            memberInfo,
        });
        await this.handleGetCoupon();
    }
})